package com.example.controller.product;

import com.alibaba.excel.EasyExcel;
import com.example.pojo.Product;
import com.example.service.product.ProductService;
import com.example.utility.ProListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: demo
 * @description: 商品表
 * @author: yc
 * @create: 2022-11-12 16:53
 **/
@CrossOrigin
@RestController
@RequestMapping("/Product")
public class ProductController {

    @Autowired
    private ProductService service;


    /**
     * @Description: 全查商品
     * @Param: categroyId
     * @return: java.util.List<com.example.pojo.Product>
     * @Author: yc
     * @Date: 2022/11/12 16:38
     */
    @RequestMapping("/getProduct")
    public List<Product> getProduct(Integer categroyId){
        List<Product> product = service.getProduct(categroyId);
        System.out.println(product);
        return product;
    }


    /**
     * @Description: 修改商品
     * @Param: Product
     * @return: int
     * @Author: yc
     * @Date: 2022/11/12 16:40
     */
    @RequestMapping("/upProduct")
    public int upProduct(Product product){
        System.out.println(product);
        return service.upProduct(product);
    }


    /**
     * @Description: 删除商品
     * @Param: producctId
     * @return: void
     * @Author: yc
     * @Date: 2022/11/12 15:18
     */
    @RequestMapping("/delProduct")
    public void delProduct(Integer producctId){

        service.delProduct(producctId);
    }


    /**
     * @Description: 添加商品
     * @Param: product
     * @return: void
     * @Author: yc
     * @Date: 2022/11/12 15:20
     */
    @RequestMapping("/addProduct")
    public int addProduct(@RequestBody Product product){
        return service.addProduct(product);
    }

    /**
    * @Description: 修改商品状态
    * @Param: * @param null
    * @return:
    * @Author: yc
    * @Date: 2022/11/15 11:28
    */
    @RequestMapping("/upproductSate")
    public int upproductSate(Integer productId,Integer productSate){
        int i = service.upproductSate(productId, productSate);
        System.out.println(productId);
        System.out.println(productSate);
        return i;
    }


    /**
     * 根据商品状态查询
     * @Param: * @param productSate
     * @return: java.util.List<com.example.pojo.Product>
     * @Author: yc
     * @Date: 2022/11/21 20:07
     */
    @RequestMapping("/getproductSate")
    public List<Product> getproductSate(Integer productSate){
        List<Product> products = service.getproductSate(productSate);
        return products;
    }

    /**
     * 根据 商品名称查询
     * @Param: * @param productName
     * @return: java.util.List<com.example.pojo.Product>
     * @Author: yc
     * @Date: 2022/11/29 14:45
     */
    @RequestMapping("/getProductAll")
    public List<Product> getProductAll(String productName){
        List<Product> productAll = service.getProductAll(productName);
        System.out.println(productAll);
        return productAll;
    }

    /**
     * 根据商品分类查询
     * @Param: * @param categroyId
     * @return: java.util.List<com.example.pojo.Product>
     * @Author: yc
     * @Date: 2022/11/29 16:28
     */
    @RequestMapping("/getCategroyId")
    public List<Product> getCategroyId(Integer categroyId){
        List<Product> categroyId1 = service.getCategroyId(categroyId);
        return categroyId1;
    }

    /**
     * @Description: 高级检索
     * @Param: * @param product
     * @return: java.util.List<com.example.pojo.Product>
     * @Author: yc
     * @Date: 2023/2/2 9:52
     */
    @RequestMapping("/selectProduct")
    public List<Product> selectProduct(Integer productCode,String productName,Integer currentpriceProduct,Integer hits) {
        List<Product> products = service.selectProduct(productCode,productName,currentpriceProduct,hits);
        return products;
    }

   /* //导出
    @RequestMapping("/StudentExcel")
    public void StudentExcel(){
        String fileName = "D:\\idea daima\\ExcelEquipment\\Excel\\StudentExcel.xlsx";
        EasyExcel.write(fileName, Product.class).sheet("模板").doWrite(service.getDaochu());
    }*/


    /**
     * @Description: 导入
     * @Param: * @param file
     * @return: java.util.Map<java.lang.String,java.lang.String>
     * @Author: yc
     * @Date: 2022/12/9 11:11
     */
    @RequestMapping("/upload")
    public Map<String,String> upload(MultipartFile file, HttpServletResponse response) throws IOException {
        // 允许跨域访问的域名：若有端口需写全（协议+域名+端口），若没有端口末尾不用加'/'
        response.setHeader("Access-Control-Allow-Origin", "*");
        //* 代办允许所有方法
        response.setHeader("Access-Control-Allow-Methods", "*");
        // Access-Control-Max-Age 用于 CORS 相关配置的缓存
        response.setHeader("Access-Control-Max-Age", "3600");
        // 提示OPTIONS预检时，后端需要设置的两个常用自定义头
        response.setHeader("Access-Control-Allow-Headers", "*");
        // 允许前端带认证cookie：启用此项后，上面的域名不能为'*'，必须指定具体的域名，否则浏览器会提示
        response.setHeader("Access-Control-Allow-Credentials", "true");

        System.out.println(file);
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream,Product.class,new ProListener(service)).sheet().doRead();

        Map<String,String> map = new HashMap<>();
        map.put("state","200");
        return map;
    }
}
